﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using Entity;

namespace SalesVMT
{
    public partial class frmCompany : Form
    {
        CompanyInfo Companyinf;
        public frmCompany(int _Company_ID)
        {
            InitializeComponent();
            Companyinf = new CompanyInfo();
            cboCurrency.SelectedIndex = 0;
            cboCity.SelectedIndex = 0;
            if (_Company_ID > 0)
            {
                Companyinf = CompanyDB.GetInfo(_Company_ID);
                Companyinf.Company_ID = (_Company_ID);
                InfoToForm();
            }
            
            this.KeyPreview = true;
        }
        private void FormToInfo()
        {
            Companyinf.Company_Code = txtCode.Text;
            Companyinf.Company_Name = txtName.Text;
            Companyinf.Company_Address = txtAddress.Text;
            Companyinf.Company_Tel = txtTel.Text;
            Companyinf.Company_Fax = txtFax.Text;
            Companyinf.Company_Email = txtEmail.Text;
            Companyinf.Company_Website = txtWeb.Text;
            Companyinf.Company_Currency = cboCurrency.Text;
            Companyinf.Company_City = cboCity.Text;
            Companyinf.Company_PriceDifference = Convert.ToDecimal(txtPriceDifference.Text);
        }
        private void InfoToForm()
        {
            txtCode.Text = Companyinf.Company_Code;
            txtName.Text = Companyinf.Company_Name;
            txtAddress.Text = Companyinf.Company_Address;
            txtTel.Text = Companyinf.Company_Tel;
            txtFax.Text = Companyinf.Company_Fax;
            txtEmail.Text = Companyinf.Company_Email;
            txtWeb.Text = Companyinf.Company_Website;
            cboCurrency.Text = Companyinf.Company_Currency;
            cboCity.Text = Companyinf.Company_City;
            txtPriceDifference.Text = Companyinf.Company_PriceDifference.ToString("#,##0.00");
        }
        private bool CheckValid()
        {
            if (!CheckTextValid.IsNumeric(txtPriceDifference.Text))
            {
                MessageBox.Show("Giá chênh lệch không hợp lệ! Hãy nhập lại", Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPriceDifference.Focus();
                txtPriceDifference.Text = "0.0";
                return false;
            }
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if(!CheckValid()) return;
            FormToInfo();
            if (Companyinf.Company_ID > 0)//Update
            {
                if (CompanyDB.Update(Companyinf))
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Update_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Info, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else //Create
            {
                Companyinf.Company_ID = CompanyDB.Insert(Companyinf);
                if (Companyinf.Company_ID > 0)
                {
                    Program.taskbarNotifier.Show(Message.Info, Message.Create_Complete, UIParamater.TimeToShow, UIParamater.TimeToStay, UIParamater.TimeToHide);
                }
                else
                {
                    MessageBox.Show(Message.Update_Error, Message.Warning, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            
            this.Close();
        }

        private void frmCompany_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Escape:
                    
                    this.Close();
                    break;
            }
        }

        private void txtPriceDifference_Leave(object sender, EventArgs e)
        {
            decimal dec = 0;
            if (!Decimal.TryParse(txtPriceDifference.Text, out dec) || dec < 0 || dec > 100)
                txtPriceDifference.Undo();
            else
                txtPriceDifference.Text = dec.ToString("#,##0.00");
        }
  
    }
}